define(["jquery", "template", "bootstrap","bbcare",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.ui.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.event.js",
    "scprit/department/vm/assessmentScreening/assessmentScreeningQuestion.servers.js"], function ($, template, bs ,$bb,$ui,$event,$servers) {
    var page = {};
    page.init = function(){
        var getParams = $bb.getPageParams();
        var type = getParams.type;
        var userId = getParams.userId;
        var answerId = getParams.answerId;
        var topbars = [];
        //获取题目：
        var paramsQuestion = $servers.getParams(type,userId);
        var quesFn = function (data){
            var resData,result;
            if(type == "HighriskPregnant"){
                resData = $servers.dataDeal(data.data);
            }else{
                resData = data.data;
            }
            if(answerId){
                result =  $.parseJSON( getParams.answer.result);
                var answerResult =  $.parseJSON(getParams.answer.answerResult);
                resData = $servers.quesStatus(resData,answerResult);
                if(type == "HighriskPregnant"){
                    $ui.highRiskInit({"id":"#question","data":resData});
                }else{
                    $ui.anxietyInit({"id":"#question","data":resData});
                }
                topbars = [
                    {
                        "icon":"fa fa-check-circle",
                        "name":"查看结果",
                        "callback":getResult,
                        "params":{"type":type,"attr":"view","userId":userId,"result":result}
                    }
                ];
                $bb.toolbar(topbars);
                $("input").attr("disabled",true);
            }else{
                if(type == "HighriskPregnant"){
                    $ui.highRiskInit({"id":"#question","data":resData});
                }else{
                    $ui.anxietyInit({"id":"#question","data":resData});
                }
                topbars = [
                    {
                        "icon":"fa fa-check-circle",
                        "name":"保存记录并返回结果",
                        "callback":getResult,
                        "params":{"type":type,"attr":"get","userId":userId,"data":data.data}
                    }
                ];
                $bb.toolbar(topbars);
                $event.jump(type);
            }
        };
        $servers.getQuestion(paramsQuestion,quesFn );
    };
    var getResult = function(){
        var params = $(this).data("params");
        var attr = params.attr,type = params.type,userId = params.userId,data = params.data,result = params.result;
        if(type == "HighriskPregnant"){
            if(attr == "view"){$('#myModal').modal('show');}
            attr == "get" ? $event.countHighRiskScore(userId,type): $ui.highRiskResultInit({"id":"#resShow","data":result.map});
        }else{
            attr == "get" ? $event.countAdpScore(userId,data.length,type): $servers.scriptAnswer("#resShow",result.map,type);
        }
    };
    return page;
});